package com.yf.service.course.impl;

import com.yf.entity.course.LessonOrderItemEntity;
import com.yf.service.course.LessonOrderService;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yf.dao.course.LessonDao;
import com.yf.entity.course.LessonEntity;
import com.yf.service.course.LessonService;

/**
 * 章节Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Oct 23 16:19:27 CST 2018
 */
@Service
public class LessonServiceImpl extends BaseServiceImpl<LessonEntity> implements LessonService {
    @Autowired
    private LessonDao lessonDao;

    @Autowired
    private LessonOrderService lessonOrderService;

    /**内部方法**/


    /**外部方法实现**/

    @Override
    public List<LessonEntity> getList(Long courseId ,Long userId) {
        List<LessonEntity> lessonList = lessonDao.getList(courseId);
        //查询章节购买人数
        getCourseLearningNumbers(lessonList);

        //查询章节的购买状态和视频地址
        getPayStatusAndVideoUrl(lessonList,userId);
        return lessonList;

    }

    @Override
    public Integer getCount(Long courseId) {
        return lessonDao.getCount(courseId);
    }

    @Override
    public List<LessonBackVo> lessonList(Map<String, Object> params) {
        return lessonDao.lessonList(params);
    }

    @Override
    public LessonVideoVO lessonVideo(Long userId, Long lessonId) {
        List<Long> list = new ArrayList<>();
        list.add(lessonId);
        List<LessonOrderItemEntity> itemList = lessonDao.lessonPaid(userId,list);
        LessonVideoVO lessonVideoVO = new LessonVideoVO();
        if(!itemList.isEmpty()){
            LessonEntity lessonEntity = lessonDao.get(lessonId);
            lessonVideoVO.setIsPaid(true);
            lessonVideoVO.setVideoUrl(lessonEntity.getVideoUrl());
        }else {
            lessonVideoVO.setIsPaid(false);
        }
        return lessonVideoVO;
    }

    @Override
    public List<LearningNumber> getLearningNumber(List<Long> lessonIds) {
        return lessonDao.getLearningNumber(lessonIds);
    }
    public List<OptCourseBackVo> courseList() {
        return lessonDao.courseList();

    }

    @Override
    public LessonEntity getLesson(Long lessonId,Long userId) {

        LessonEntity lessonEntity = lessonDao.getLesson(lessonId); //获取lesson信息
        List<LessonEntity> list = new ArrayList<>();
        list.add(lessonEntity);
        System.out.println("------------userId------"+userId);
        System.out.println("------------lessonId-------------------"+lessonId);
        //查询章节的购买人数(学习人数)
        getCourseLearningNumbers(list);

        //查询视频的购买状态和地址
        getPayStatusAndVideoUrl(list,userId);

        return list.get(0);
    }

    //查询章节购买人数
    private void getCourseLearningNumbers(List<LessonEntity> list){
        if(!list.isEmpty()){
            List lessonIds = new ArrayList();
            for (LessonEntity lessonEntity : list) {
                lessonIds.add(lessonEntity.getId());
            }
            //查询章节列表每个章节的购买人数
            List<LearningNumber> learningNumbers = lessonDao.getLearningNumber(lessonIds);
            if(!learningNumbers.isEmpty()){
                for (LessonEntity lessonEntity : list) {
                    for (LearningNumber learningNumber : learningNumbers) {
                        if(learningNumber.getId().equals(lessonEntity.getId())){
                            lessonEntity.setLearningNumber(learningNumber.getCount());
                            break;
                        }
                    }
                }
            }else {
                for (LessonEntity lessonEntity : list) {
                    lessonEntity.setLearningNumber(0);
                }
            }

        }

    }

    @Override
    public List<String> getLessonUrlByCourseId(Long courseId) {
        return lessonDao.getLessonUrlByCourseId(courseId);
    }


    //查询章节的购买状态和视频地址
    private void getPayStatusAndVideoUrl(List<LessonEntity> lessonList,Long userId){
        //查询章节的购买状态和视频地址
        List<Long> paidLessonList = lessonOrderService.getLessonByUser(userId);
        if(!paidLessonList.isEmpty()){
            List<LessonEntity> list = lessonDao.getListByIds(paidLessonList);
            for (LessonEntity lessonEntity : lessonList) {
                for (LessonEntity entity : list) {
                    if(lessonEntity.getId().equals(entity.getId())){
                        lessonEntity.setPayStatus(true);
                        lessonEntity.setVideoUrl(entity.getVideoUrl());
                        break;
                    }
                }
            }
        }
    }

    @Override
    public List<LessonRecordVO> getPaidLessonList(Long userId) {
        return lessonDao.getPaidLessonList(userId);
    }
}
